package com.lcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcj.dto.User;
import com.lcj.dto.UserLogin;
import com.lcj.dto.UserReg;
import com.lcj.dto.UserUpdate;
import com.lcj.entity.Users;
import com.lcj.exception.UserExistException;
import com.lcj.mapper.UserMapper;
import com.lcj.service.IUserService;
import com.lcj.utils.JwtService;
import com.lcj.utils.UserUtils;
import com.lcj.vo.UserInfo;
import com.lcj.vo.UserVo;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtService jwtService;

    @Override
    public UserInfo register(UserReg userReg) {

        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("email", userReg.getUserInfo().getEmail());
        Users users = userMapper.selectOne(usersQueryWrapper);
        if (users == null) {

            String password = userReg.getUserInfo().getPassword();
            String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
            Users userEntity = new Users();
            userEntity.setEmail(userReg.getUserInfo().getEmail());
            userEntity.setPassword(hashpw);
            userEntity.setUsername(userReg.getUserInfo().getUsername());
            userMapper.insert(userEntity);
            System.out.println(hashpw);
            UserInfo userInfo = new UserInfo();
            UserVo userVo = new UserVo();
            userVo.setEmail(userReg.getUserInfo().getEmail());
            Long id = userEntity.getId();
            String token = jwtService.toToken(id.toString());
            userVo.setToken(token);
            userVo.setUsername(userReg.getUserInfo().getUsername());
            userInfo.setUserVo(userVo);
            return userInfo;
        } else {
            throw new UserExistException(600, "邮箱已存在");
        }


    }

    @Override
    public UserInfo login(UserLogin userLogin) {
        if (userLogin.getUser() == null) {
            throw new RuntimeException("用户信息为空");

        } else {
            String email = userLogin.getUser().getEmail();
            String password1 = userLogin.getUser().getPassword();
            if (email == null || password1 == null) {
                throw new RuntimeException("邮箱或密码为空");

            } else {

                QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
                QueryWrapper<Users> email1 = usersQueryWrapper.eq("email", email);
                Users users = userMapper.selectOne(email1);
                if (users != null) {
                    String inputPassword = userLogin.getUser().getPassword();
                    String password = users.getPassword();
                    if (BCrypt.checkpw(inputPassword, password)) {
                        UserInfo userInfo = new UserInfo();
                        UserVo userVo = new UserVo();
                        userVo.setEmail(users.getEmail());
                        Long id = users.getId();
                        String token = jwtService.toToken(id.toString());
                        userVo.setToken(token);
                        userVo.setUsername(users.getUsername());
                        userVo.setBio(users.getBio());
                        userVo.setImage(users.getImage());
                        userInfo.setUserVo(userVo);
                        return userInfo;
                    } else {
                        throw new UserExistException(601, "密码错误");
                    }
                } else {
                    throw new UserExistException(602, "用户不存在");
                }
            }


        }
    }

    @Override
    public UserInfo getUserInfo(String token) {
        System.out.println("lcj");
        Users loginUser = UserUtils.getLoginUser();
        System.out.println(loginUser);
        UserInfo userInfo = new UserInfo();
        UserVo userVo = new UserVo();
        userVo.setEmail(loginUser.getEmail());
        userVo.setToken(token);
        userVo.setUsername(loginUser.getUsername());
        userVo.setBio(loginUser.getBio());
        userVo.setImage(loginUser.getImage());
        userInfo.setUserVo(userVo);
        return userInfo;
    }

    @Override
    public UserInfo update(String token, UserUpdate userUpdate) {
        User user1 = userUpdate.getUser();
        if (user1 == null) {
            throw new RuntimeException("用户更新信息为空");
        } else {
            Users loginUser = UserUtils.getLoginUser();
            Long id = loginUser.getId();
            QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
            usersQueryWrapper.eq("id", id);
            Users users = new Users();
            users.setId(id);
            if (user1.getEmail() != null) {
                users.setEmail(user1.getEmail());
            }
            if (user1.getUsername() != null) {
                users.setUsername(user1.getUsername());
            }
            if (user1.getBio() != null) {
                users.setBio(user1.getBio());
            }
            if (user1.getImage() != null) {
                users.setImage(user1.getImage());
            }
            if (user1.getPassword() != null) {
                String hashpw = BCrypt.hashpw(user1.getPassword(), BCrypt.gensalt());
                users.setPassword(hashpw);
            }
            userMapper.update(users, usersQueryWrapper);
            UserInfo userInfo = new UserInfo();
            UserVo userVo = new UserVo();
            userVo.setEmail(user1.getEmail());
            userVo.setUsername(user1.getUsername());
            userVo.setBio(user1.getBio());
            userVo.setImage(user1.getImage());
            userVo.setToken(token);
            userInfo.setUserVo(userVo);


            return userInfo;
        }
    }


}
